package keeto.com.vn.bms.service;

import java.util.ArrayList;

import keeto.com.vn.bms.config.BMSCore;
import keeto.com.vn.bms.config.IBMSConfig;
import keeto.com.vn.bms.model.InfoMessegerModel;
import keeto.com.vn.bms.model.NotificationModel;
import keeto.com.vn.bms.network.IBMSProtocol;
import keeto.com.vn.bms.ui.activity.BMSChatActivity;
import keeto.com.vn.bms.ui.activity.HomeBMSActivity;
import keeto.com.vn.bms.ui.activity.R;
import keeto.com.vn.bms.ui.activity.SplashScreenActivity;
import keeto.com.vn.bms.utils.BMSListener;
import keeto.com.vn.bms.utils.BMSSharedPreferences;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.util.Log;

public class BMSService extends Service {

	public static final String ACTION_SET_TIME_CONNECTION = "keeto.com.vn.bms.service.BMSService.time";
	public static final String ACTION_GET_NOTIFY = "keeto.com.vn.bms.service.BMSService.Notify";

	private NotificationCompat.Builder builder[];
	private Listener listener = new Listener();
	private BMSSharedPreferences preferences;
	private final int PAGE_LIMIT_INFO = 10;
	private int PageNumber = 1;
	private PendingIntent pendingIntent;
	private Context context;
	private String mAction;
	private String AuthKey;
	private int Permisstion;
	private Intent it;

	public static void lunchServicegetTimeConnection(Context context){
		Intent intent = new Intent(context, BMSService.class);
		intent.setAction(ACTION_SET_TIME_CONNECTION);
		context.startService(intent);
	}
	public static void lunchServicegetNotify(Context context){
		Intent intent = new Intent(context, BMSService.class);
		intent.setAction(ACTION_GET_NOTIFY);
		context.startService(intent);
	}
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// TODO Auto-generated method stub
		if (intent != null && intent.getAction() != null){
			mAction = intent.getAction();
			context = this.getApplication();
			preferences = new BMSSharedPreferences(context);
			if (mAction.equals(ACTION_SET_TIME_CONNECTION)){
				BMSCore.getInstance(context).GetTimeNotification(listener);
			}else if (mAction.equals(ACTION_GET_NOTIFY)){

				AuthKey = preferences.getUserAuthKey(context);
				Permisstion = preferences.getUserPermission(context);
				if (!AuthKey.equals("")){
					if (Permisstion != IBMSConfig.KEY_TYPE_LEADERSHIP){
						Log.e("Service "," get Notify");
						BMSCore.getInstance(getApplicationContext()).taskGetListNotify(AuthKey, PAGE_LIMIT_INFO, PageNumber, listener);
					}
					if (( Permisstion != IBMSConfig.KEY_TYPE_MARKETING) && (Permisstion != IBMSConfig.KEY_TYPE_TECHNICIAL )){
						Log.e("Service "," get msg");
						BMSCore.getInstance(context).taskGetListMesseger(1, PAGE_LIMIT_INFO, PageNumber, AuthKey, listener);
					}
				}

			}
		}
		return super.onStartCommand(intent, flags, startId);
	}

	@Override
	public void onCreate() {
		super.onCreate();

	}

	@Override
	public void onDestroy() {
		super.onDestroy();

	}

	class Listener extends BMSListener{
		@Override
		public void finishGetListNotification(final int returnCode, int pageCount, final ArrayList<NotificationModel> arrayList) {
			super.finishGetListNotification(returnCode, pageCount, arrayList);

			if (returnCode == IBMSProtocol.STATUS_CODE_OK && arrayList != null){
				builder = new NotificationCompat.Builder[arrayList.size()];
				if(arrayList!= null && arrayList.size() > 0 ){
					for (int i = 0; i < arrayList.size(); i++) {
						if(!arrayList.get(i).isRead()){
							switch (Permisstion) {
							case IBMSConfig.KEY_TYPE_LEADERSHIP:
							case IBMSConfig.KEY_TYPE_ADMIN:
							case IBMSConfig.KEY_TYPE_DOCTOR:
								it = new Intent(context,HomeBMSActivity.class);
								it.setAction(HomeBMSActivity.ACTION_READ_NOTIFY);
								it.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK |Intent.FLAG_ACTIVITY_NEW_TASK |Intent.FLAG_ACTIVITY_NO_ANIMATION);
								it.putExtra("NotifyId", arrayList.get(i).getNotifyId());
								break;
							case IBMSConfig.KEY_TYPE_TECHNICIAL:
							case IBMSConfig.KEY_TYPE_MARKETING:
							case IBMSConfig.KEY_TYPE_CUSTOMER:
								it = new Intent(context,HomeBMSActivity.class);
								it.setAction(HomeBMSActivity.ACTION_READ_NOTIFY);
								it.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK |Intent.FLAG_ACTIVITY_NEW_TASK |Intent.FLAG_ACTIVITY_NO_ANIMATION);
								it.putExtra("NotifyId", arrayList.get(i).getNotifyId());
								break;
							default:
								break;
							}
							pendingIntent = PendingIntent.getActivity(context,0, it, 0);
							builder[i] = new NotificationCompat.Builder(getApplication());
//							builder[i].setContentTitle(arrayList.get(i).getActivitiTitle());
							builder[i].setContentText(arrayList.get(i).getNotifyContent());
							builder[i].setSmallIcon(R.drawable.ic_launcher);
							builder[i].setContentIntent(pendingIntent);
							builder[i].setAutoCancel(true);
							NotificationManagerCompat managerCompat = NotificationManagerCompat.from(context);
							managerCompat.notify(Integer.parseInt(arrayList.get(i).getNotifyId()), builder[i].build());
						}
					}
				}
			}
		}

		@Override
		public void finishGetListMesseger(int returnCode, int pageCount,
				ArrayList<InfoMessegerModel> ListMsg) {
			// TODO Auto-generated method stub
			super.finishGetListMesseger(returnCode, pageCount, ListMsg);
			if (returnCode == IBMSProtocol.STATUS_CODE_OK && ListMsg != null){
				builder = new NotificationCompat.Builder[ListMsg.size()];
				if(ListMsg!= null && ListMsg.size() > 0 ){
					for (int i = 0; i < ListMsg.size(); i++) {
						if(!ListMsg.get(i).isRead()){
							switch (Permisstion) {
							case IBMSConfig.KEY_TYPE_LEADERSHIP:
							case IBMSConfig.KEY_TYPE_ADMIN:
							case IBMSConfig.KEY_TYPE_DOCTOR:
								it = new Intent(context,BMSChatActivity.class);
								it.setAction(HomeBMSActivity.ACTION_READ_MESSAGER);
								it.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK |Intent.FLAG_ACTIVITY_NEW_TASK |Intent.FLAG_ACTIVITY_NO_ANIMATION);
								it.putExtra("MsgId", ListMsg.get(i).getMsgID());
								break;
							case IBMSConfig.KEY_TYPE_TECHNICIAL:
							case IBMSConfig.KEY_TYPE_MARKETING:
							case IBMSConfig.KEY_TYPE_CUSTOMER:
								it = new Intent(context,HomeBMSActivity.class);
								it.setAction(HomeBMSActivity.ACTION_READ_MESSAGER);
								it.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK |Intent.FLAG_ACTIVITY_NEW_TASK |Intent.FLAG_ACTIVITY_NO_ANIMATION);
								it.putExtra("MsgId", ListMsg.get(i).getMsgID());
								break;
							default:
								break;
							}
							pendingIntent = PendingIntent.getActivity(context,0, it, 0);
							builder[i] = new NotificationCompat.Builder(getApplication());
//							builder[i].setContentTitle(ListMsg.get(i).getMsgtittle());
							builder[i].setContentText(ListMsg.get(i).getMsgContent());
							builder[i].setSmallIcon(R.drawable.ic_launcher);
							builder[i].setContentIntent(pendingIntent);
							builder[i].setAutoCancel(true);
							NotificationManagerCompat managerCompat = NotificationManagerCompat.from(context);
							managerCompat.notify(Integer.parseInt(ListMsg.get(i).getMsgID()), builder[i].build());
						}
					}
				}
			}
		}

		@Override
		public void finishGetTimeNotification(int reponse) {
			// TODO Auto-generated method stub
			super.finishGetTimeNotification(reponse);
			if(reponse != 0){
				Intent intent = new Intent(context,BMSReceiver.class);
				intent.setAction(BMSReceiver.BMSRECEIVER_GETNOTIFY);
				intent.putExtra("TimeConnection", reponse);
				if (preferences!= null){
					preferences.setStartNotify(reponse);
				}
				context.sendBroadcast(intent);
			}
		}
	}

}
